package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.result.PageResult;
import com.heima.mapper.PolicyMapper;
import com.heima.pojo.dto.PolicyQueryDTO;
import com.heima.pojo.entity.Policy;
import com.heima.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 策略服务实现类
 */
@Service
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements PolicyService {

    @Autowired
    private PolicyMapper policyMapper;
    /**
     * 策略条件查询
     * @param policyDTO
     * @return
     */
    @Override
    public PageResult<Policy> pageResult(PolicyQueryDTO policyDTO) {

        // 分页查询
        Page<Policy> page = new Page<>(policyDTO.getPageIndex(), policyDTO.getPageSize());

        LambdaQueryWrapper<Policy> wrapper = new LambdaQueryWrapper<Policy>()
                .like(StringUtils.isNotBlank(policyDTO.getPolicyName()), Policy::getPolicyName, policyDTO.getPolicyName());

        Page<Policy> policyPage = this.page(page, wrapper);

        return PageResult.<Policy>builder()
                .totalCount((int)policyPage.getTotal())
                .totalPage((int)policyPage.getPages())
                .pageIndex(policyDTO.getPageIndex())
                .pageSize(policyDTO.getPageSize())
                .currentPageRecords(policyPage.getRecords())
                .build();
    }
}